<!DOCTYPE html>
<html lang="zh_CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <meta name="generator" content="VuePress 2.0.0-beta.45">
    <style>
      :root {
        --c-bg: #fff;
      }
      html.dark {
        --c-bg: #22272e;
      }
      html, body {
        background-color: var(--c-bg);
      }
    </style>
    <script>
      const userMode = localStorage.getItem('vuepress-color-scheme');
			const systemDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
			if (userMode === 'dark' || (userMode !== 'light' && systemDarkMode)) {
				document.documentElement.classList.toggle('dark', true);
			}
    </script>
    <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico"><title>Logrus 中文文档 | 科科人神</title><meta name="description" content="科科人神，后端软件工程师，喜欢云原生">
    <link rel="modulepreload" href="/assets/app.80e5dc69.js"><link rel="modulepreload" href="/assets/index.html.f8c39be7.js"><link rel="modulepreload" href="/assets/index.html.5d63961d.js"><link rel="prefetch" href="/assets/index.html.cba988ab.js"><link rel="prefetch" href="/assets/1.html.8ac90e92.js"><link rel="prefetch" href="/assets/19.html.4444be95.js"><link rel="prefetch" href="/assets/必备知识.html.7da85c42.js"><link rel="prefetch" href="/assets/必备知识罗列.html.5e18db77.js"><link rel="prefetch" href="/assets/1.html.356f0755.js"><link rel="prefetch" href="/assets/2.html.085440fc.js"><link rel="prefetch" href="/assets/3.html.09effb5c.js"><link rel="prefetch" href="/assets/1.html.50fb5135.js"><link rel="prefetch" href="/assets/1.html.17222973.js"><link rel="prefetch" href="/assets/1.html.0a197ef1.js"><link rel="prefetch" href="/assets/4-1.html.073fbd99.js"><link rel="prefetch" href="/assets/4-2.html.64039343.js"><link rel="prefetch" href="/assets/4.html.021c0881.js"><link rel="prefetch" href="/assets/5-6-1.html.34978ed1.js"><link rel="prefetch" href="/assets/5-6-2.html.f004f560.js"><link rel="prefetch" href="/assets/5-6.html.f5ad3835.js"><link rel="prefetch" href="/assets/index.html.467e6eba.js"><link rel="prefetch" href="/assets/index.html.f3622043.js"><link rel="prefetch" href="/assets/index.html.a2792a89.js"><link rel="prefetch" href="/assets/index.html.0b5fc023.js"><link rel="prefetch" href="/assets/index.html.2f735cf1.js"><link rel="prefetch" href="/assets/index.html.9d71a6d6.js"><link rel="prefetch" href="/assets/bash_completions.html.ed68147e.js"><link rel="prefetch" href="/assets/man_docs.html.2f0dd0c9.js"><link rel="prefetch" href="/assets/md_docs.html.20de452a.js"><link rel="prefetch" href="/assets/rest_docs.html.b77e8a2e.js"><link rel="prefetch" href="/assets/zsh_completions.html.60976ab2.js"><link rel="prefetch" href="/assets/syslog.html.48554683.js"><link rel="prefetch" href="/assets/daily_dep.html.2f155f5c.js"><link rel="prefetch" href="/assets/getting_started.html.769a2eaa.js"><link rel="prefetch" href="/assets/installation.html.e6b0002b.js"><link rel="prefetch" href="/assets/migrating.html.128871c1.js"><link rel="prefetch" href="/assets/new_project.html.8326d390.js"><link rel="prefetch" href="/assets/uninstalling.html.26650652.js"><link rel="prefetch" href="/assets/deduction.html.d4acd3fa.js"><link rel="prefetch" href="/assets/ensure_mechanics.html.603c62de.js"><link rel="prefetch" href="/assets/404.html.a15b4cbd.js"><link rel="prefetch" href="/assets/index.html.29aac47c.js"><link rel="prefetch" href="/assets/index.html.b64fbb23.js"><link rel="prefetch" href="/assets/index.html.cd48e230.js"><link rel="prefetch" href="/assets/index.html.6b048e76.js"><link rel="prefetch" href="/assets/index.html.7f0a6e54.js"><link rel="prefetch" href="/assets/index.html.b5a71586.js"><link rel="prefetch" href="/assets/index.html.60805430.js"><link rel="prefetch" href="/assets/index.html.60736b0b.js"><link rel="prefetch" href="/assets/index.html.80dddd6d.js"><link rel="prefetch" href="/assets/index.html.cc3c6a0d.js"><link rel="prefetch" href="/assets/index.html.44e62fda.js"><link rel="prefetch" href="/assets/index.html.015a0b2c.js"><link rel="prefetch" href="/assets/index.html.415d4517.js"><link rel="prefetch" href="/assets/index.html.d1d65e23.js"><link rel="prefetch" href="/assets/index.html.725ac526.js"><link rel="prefetch" href="/assets/index.html.c824ee30.js"><link rel="prefetch" href="/assets/1.html.4fb25663.js"><link rel="prefetch" href="/assets/19.html.ded8a9c9.js"><link rel="prefetch" href="/assets/必备知识.html.0ff3a77d.js"><link rel="prefetch" href="/assets/必备知识罗列.html.85e0f635.js"><link rel="prefetch" href="/assets/1.html.4f76037b.js"><link rel="prefetch" href="/assets/2.html.05a6e91e.js"><link rel="prefetch" href="/assets/3.html.30d4c4b6.js"><link rel="prefetch" href="/assets/1.html.ce86a757.js"><link rel="prefetch" href="/assets/1.html.0f8a9434.js"><link rel="prefetch" href="/assets/1.html.f4ee2fe7.js"><link rel="prefetch" href="/assets/4-1.html.c7cc75b6.js"><link rel="prefetch" href="/assets/4-2.html.36807608.js"><link rel="prefetch" href="/assets/4.html.c03ae622.js"><link rel="prefetch" href="/assets/5-6-1.html.651e5517.js"><link rel="prefetch" href="/assets/5-6-2.html.5ccfdb5e.js"><link rel="prefetch" href="/assets/5-6.html.882bc951.js"><link rel="prefetch" href="/assets/index.html.9d9ba895.js"><link rel="prefetch" href="/assets/index.html.3da373d1.js"><link rel="prefetch" href="/assets/index.html.6ec0b380.js"><link rel="prefetch" href="/assets/index.html.e8d4dc57.js"><link rel="prefetch" href="/assets/index.html.2275c56f.js"><link rel="prefetch" href="/assets/index.html.711c721b.js"><link rel="prefetch" href="/assets/bash_completions.html.31fa6426.js"><link rel="prefetch" href="/assets/man_docs.html.cc0f68c6.js"><link rel="prefetch" href="/assets/md_docs.html.dd10674f.js"><link rel="prefetch" href="/assets/rest_docs.html.46da86e5.js"><link rel="prefetch" href="/assets/zsh_completions.html.64d57b81.js"><link rel="prefetch" href="/assets/syslog.html.5324d692.js"><link rel="prefetch" href="/assets/daily_dep.html.b7a8b70b.js"><link rel="prefetch" href="/assets/getting_started.html.29c6534a.js"><link rel="prefetch" href="/assets/installation.html.b11da95a.js"><link rel="prefetch" href="/assets/migrating.html.92825adc.js"><link rel="prefetch" href="/assets/new_project.html.8fcfe511.js"><link rel="prefetch" href="/assets/uninstalling.html.34c1a6fb.js"><link rel="prefetch" href="/assets/deduction.html.2f0a618b.js"><link rel="prefetch" href="/assets/ensure_mechanics.html.7818d8b5.js"><link rel="prefetch" href="/assets/404.html.37719150.js"><link rel="prefetch" href="/assets/index.html.bc2fbcdd.js"><link rel="prefetch" href="/assets/index.html.2363e42b.js"><link rel="prefetch" href="/assets/index.html.691f6b43.js"><link rel="prefetch" href="/assets/index.html.c73599f4.js"><link rel="prefetch" href="/assets/index.html.32559aa6.js"><link rel="prefetch" href="/assets/index.html.283a60cc.js"><link rel="prefetch" href="/assets/index.html.b72bddc9.js"><link rel="prefetch" href="/assets/index.html.aebed871.js"><link rel="prefetch" href="/assets/index.html.1bace120.js"><link rel="prefetch" href="/assets/index.html.7913d44a.js"><link rel="prefetch" href="/assets/index.html.4e5360d4.js"><link rel="prefetch" href="/assets/index.html.028748b8.js"><link rel="prefetch" href="/assets/index.html.e9cb3a86.js"><link rel="prefetch" href="/assets/index.html.e31e8159.js"><link rel="prefetch" href="/assets/index.html.01049197.js"><link rel="prefetch" href="/assets/404.30cc8863.js"><link rel="prefetch" href="/assets/HomePage.4e823a0b.js"><link rel="prefetch" href="/assets/Layout.61f5e0fd.js"><link rel="prefetch" href="/assets/Links.8680cda6.js"><link rel="prefetch" href="/assets/Post.93b09438.js"><link rel="prefetch" href="/assets/Tags.3d9bb337.js">
    <link rel="stylesheet" href="/assets/style.8b24233a.css">
  </head>
  <body>
    <div id="app"><!--[--><div class="theme-container"><!--[--><header class="navbar is-fixed is-visible invert"><span><a href="/" class=""><span class="site-name">$ cd /home/</span></a></span><div class="navbar-items-wrapper" style=""><!--[--><!--]--><nav class="navbar-items can-hide"><!--[--><div class="navbar-item"><a href="/" class="" aria-label="home"><!--[--><!--]--><span class="nav-icon"><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="0 0 20 20" fill="currentColor"><path d="M10.707 2.293a1 1 0 00-1.414 0l-7 7a1 1 0 001.414 1.414L4 10.414V17a1 1 0 001 1h2a1 1 0 001-1v-2a1 1 0 011-1h2a1 1 0 011 1v2a1 1 0 001 1h2a1 1 0 001-1v-6.586l.293.293a1 1 0 001.414-1.414l-7-7z"/></svg></span><span>home</span><!--[--><!--]--></a></div><div class="navbar-item"><a href="/tags/" class="" aria-label="归档"><!--[--><!--]--><span class="nav-icon"><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="-1.6 -1.6 19.2 19.2" fill="currentColor"><path d="M2 2a1 1 0 011-1h4.586a1 1 0 01.707.293l7 7a1 1 0 010 1.414l-4.586 4.586a1 1 0 01-1.414 0l-7-7A1 1 0 012 6.586V2zm3.5 4a1.5 1.5 0 100-3 1.5 1.5 0 000 3z"/><path d="M1.293 7.793A1 1 0 011 7.086V2a1 1 0 00-1 1v4.586a1 1 0 00.293.707l7 7a1 1 0 001.414 0l.043-.043-7.457-7.457z"/></svg></span><span>归档</span><!--[--><!--]--></a></div><div class="navbar-item"><a href="/links/" class="" aria-label="作品合集"><!--[--><!--]--><span class="nav-icon"><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="-79.52 -43.52 599.04 599.04" fill="currentColor"><path d="M382.7 292.5l2.7 2.7-170-167.3c-3.5-3.5-9.7-3.7-13.8-.5L144.3 171c-4.2 3.2-4.6 8.7-1.1 12.2l68.1 64.3c3.6 3.5 9.9 3.7 14 .5l.1-.1c4.1-3.2 10.4-3 14 .5l84 81.3c3.6 3.5 3.2 9-.9 12.2l-93.2 74c-4.2 3.3-10.5 3.1-14.2-.4L63.2 268c-3.5-3.5-9.7-3.7-13.9-.5L3.5 302.4c-4.2 3.2-4.7 8.7-1.2 12.2L211 510.7s7.4 6.8 17.3-.8l198-163.9c4-3.2 4.4-8.7.7-12.2zm54.5-83.4L226.7 2.5c-1.5-1.2-8-5.5-16.3 1.1L3.6 165.7c-4.2 3.2-4.8 8.7-1.2 12.2l42.3 41.7 171.7 165.1c3.7 3.5 10.1 3.7 14.3.4l50.2-38.8-.3-.3 7.7-6c4.2-3.2 4.6-8.7.9-12.2l-57.1-54.4c-3.6-3.5-10-3.7-14.2-.5l-.1.1c-4.2 3.2-10.5 3.1-14.2-.4L109 180.8c-3.6-3.5-3.1-8.9 1.1-12.2l92.2-71.5c4.1-3.2 10.3-3 13.9.5l160.4 159c3.7 3.5 10 3.7 14.1.5l45.8-35.8c4.1-3.2 4.4-8.7.7-12.2z"/></svg></span><span>作品合集</span><!--[--><!--]--></a></div><!--]--><div class="navbar-item"><a style="cursor:pointer;"><span class="nav-icon"><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="0 0 24 24" fill="currentColor"><path fill="none" d="M0 0h24v24H0z"/><path d="M11 2c4.968 0 9 4.032 9 9s-4.032 9-9 9-9-4.032-9-9 4.032-9 9-9zm0 16c3.867 0 7-3.133 7-7 0-3.868-3.133-7-7-7-3.868 0-7 3.132-7 7 0 3.867 3.132 7 7 7zm8.485.071l2.829 2.828-1.415 1.415-2.828-2.829 1.414-1.414z"/></svg></span><span>搜索</span></a></div></nav><!--[--><!--]--><!----></div></header><!--]--><div class="sidebar-mask"></div><!--[--><aside class="sidebar"><nav class="navbar-items"><!--[--><div class="navbar-item"><a href="/" class="" aria-label="home"><!--[--><!--]--><span class="nav-icon"><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="0 0 20 20" fill="currentColor"><path d="M10.707 2.293a1 1 0 00-1.414 0l-7 7a1 1 0 001.414 1.414L4 10.414V17a1 1 0 001 1h2a1 1 0 001-1v-2a1 1 0 011-1h2a1 1 0 011 1v2a1 1 0 001 1h2a1 1 0 001-1v-6.586l.293.293a1 1 0 001.414-1.414l-7-7z"/></svg></span><span>home</span><!--[--><!--]--></a></div><div class="navbar-item"><a href="/tags/" class="" aria-label="归档"><!--[--><!--]--><span class="nav-icon"><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="-1.6 -1.6 19.2 19.2" fill="currentColor"><path d="M2 2a1 1 0 011-1h4.586a1 1 0 01.707.293l7 7a1 1 0 010 1.414l-4.586 4.586a1 1 0 01-1.414 0l-7-7A1 1 0 012 6.586V2zm3.5 4a1.5 1.5 0 100-3 1.5 1.5 0 000 3z"/><path d="M1.293 7.793A1 1 0 011 7.086V2a1 1 0 00-1 1v4.586a1 1 0 00.293.707l7 7a1 1 0 001.414 0l.043-.043-7.457-7.457z"/></svg></span><span>归档</span><!--[--><!--]--></a></div><div class="navbar-item"><a href="/links/" class="" aria-label="作品合集"><!--[--><!--]--><span class="nav-icon"><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="-79.52 -43.52 599.04 599.04" fill="currentColor"><path d="M382.7 292.5l2.7 2.7-170-167.3c-3.5-3.5-9.7-3.7-13.8-.5L144.3 171c-4.2 3.2-4.6 8.7-1.1 12.2l68.1 64.3c3.6 3.5 9.9 3.7 14 .5l.1-.1c4.1-3.2 10.4-3 14 .5l84 81.3c3.6 3.5 3.2 9-.9 12.2l-93.2 74c-4.2 3.3-10.5 3.1-14.2-.4L63.2 268c-3.5-3.5-9.7-3.7-13.9-.5L3.5 302.4c-4.2 3.2-4.7 8.7-1.2 12.2L211 510.7s7.4 6.8 17.3-.8l198-163.9c4-3.2 4.4-8.7.7-12.2zm54.5-83.4L226.7 2.5c-1.5-1.2-8-5.5-16.3 1.1L3.6 165.7c-4.2 3.2-4.8 8.7-1.2 12.2l42.3 41.7 171.7 165.1c3.7 3.5 10.1 3.7 14.3.4l50.2-38.8-.3-.3 7.7-6c4.2-3.2 4.6-8.7.9-12.2l-57.1-54.4c-3.6-3.5-10-3.7-14.2-.5l-.1.1c-4.2 3.2-10.5 3.1-14.2-.4L109 180.8c-3.6-3.5-3.1-8.9 1.1-12.2l92.2-71.5c4.1-3.2 10.3-3 13.9.5l160.4 159c3.7 3.5 10 3.7 14.1.5l45.8-35.8c4.1-3.2 4.4-8.7.7-12.2z"/></svg></span><span>作品合集</span><!--[--><!--]--></a></div><!--]--><div class="navbar-item"><a style="cursor:pointer;"><span class="nav-icon"><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="0 0 24 24" fill="currentColor"><path fill="none" d="M0 0h24v24H0z"/><path d="M11 2c4.968 0 9 4.032 9 9s-4.032 9-9 9-9-4.032-9-9 4.032-9 9-9zm0 16c3.867 0 7-3.133 7-7 0-3.868-3.133-7-7-7-3.868 0-7 3.132-7 7 0 3.867 3.132 7 7 7zm8.485.071l2.829 2.828-1.415 1.415-2.828-2.829 1.414-1.414z"/></svg></span><span>搜索</span></a></div></nav><!--[--><!--]--><ul class="sidebar-items"><!--[--><li><p tabindex="0" class="sidebar-item sidebar-heading">Logrus 中文文档 <!----></p><!----></li><!--]--></ul><!--[--><!--]--></aside><!--]--><div class="page-content"><!--[--><main class="page"><!--[--><div class="article-header" style=""><!----><div class="article-header-content"><!----><h1 class="article-title"></h1><!----><div class="article-icons"><div class="article-icon"><svg class="ov-icon" style="font-size:1.08em;" aria-hidden="true" width="17.28" height="17.28" viewbox="-75.52 -43.52 599.04 599.04" fill="currentColor"><path d="M313.6 304c-28.7 0-42.5 16-89.6 16-47.1 0-60.8-16-89.6-16C60.2 304 0 364.2 0 438.4V464c0 26.5 21.5 48 48 48h352c26.5 0 48-21.5 48-48v-25.6c0-74.2-60.2-134.4-134.4-134.4zM400 464H48v-25.6c0-47.6 38.8-86.4 86.4-86.4 14.6 0 38.3 16 89.6 16 51.7 0 74.9-16 89.6-16 47.6 0 86.4 38.8 86.4 86.4V464zM224 288c79.5 0 144-64.5 144-144S303.5 0 224 0 80 64.5 80 144s64.5 144 144 144zm0-240c52.9 0 96 43.1 96 96s-43.1 96-96 96-96-43.1-96-96 43.1-96 96-96z"/></svg><span>科科人神</span></div><!----><div class="article-icon"><svg class="ov-icon" style="font-size:1.08em;" aria-hidden="true" width="17.28" height="17.28" viewbox="0 0 24 24" fill="currentColor"><path fill="none" d="M0 0h24v24H0z"/><path d="M17.618 5.968l1.453-1.453 1.414 1.414-1.453 1.453a9 9 0 11-1.414-1.414zM12 20a7 7 0 100-14 7 7 0 000 14zM11 8h2v6h-2V8zM8 1h8v2H8V1z"/></svg><span>16 min</span></div></div></div><!----></div><!--[--><!--]--><!--]--><div class="theme-gungnir-content"><!--[--><!--]--><!--[--><h1 id="logrus-中文文档" tabindex="-1"><a class="header-anchor" href="#logrus-中文文档" aria-hidden="true">#</a> Logrus 中文文档</h1><p>Logrus <img src="http://i.imgur.com/hTeVwmJ.png" width="40" height="40" alt=":walrus:" class="emoji" title=":walrus:"> <a href="https://travis-ci.org/sirupsen/logrus" target="_blank" rel="noopener noreferrer"><img src="https://travis-ci.org/sirupsen/logrus.svg?branch=master" alt="Build Status"></a> <a href="https://godoc.org/github.com/sirupsen/logrus" target="_blank" rel="noopener noreferrer"><img src="https://godoc.org/github.com/sirupsen/logrus?status.svg" alt="GoDoc"></a></p><p>Logrus 是一个用于 Go (golang) 的结构化日志模块，完全兼容标准的日志库的 API</p><p><strong>看到奇怪的大小写敏感问题？</strong> 在过去，可能同时导入大写和小写的 Logrus 。由于 Go 包环境的原因，这在社区中引起了一些问题，我们需要一个标准。 一些环境遇到了大写变体的问题，因此决定使用小写变体。 所有使用 <code>logrus</code> 的项目都需要使用小写: <code>github.com/sirupsen/logrus</code>。任何不是这样的包都应该被更改。</p><p>要修复 Glide, 查看 <a href="https://github.com/sirupsen/logrus/issues/553#issuecomment-306591437" target="_blank" rel="noopener noreferrer">these comments</a>. 关于这个问题的深入解释，查看 <a href="https://github.com/sirupsen/logrus/issues/570#issuecomment-313933276" target="_blank" rel="noopener noreferrer">this comment</a>.</p><p>在开发中使用漂亮的颜色代码(当附加 TTY 时，否则只是纯文本):</p><p><img src="http://i.imgur.com/PY7qMwd.png" alt="Colored"></p><p>使用 <code>log.SetFormatter(&amp;log.JSONFormatter{})</code>, 可以使用 <code>logstash</code> 或 <code>Splunk</code> 轻松解析：</p><div class="language-json ext-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span><span class="token property">&quot;animal&quot;</span><span class="token operator">:</span><span class="token string">&quot;walrus&quot;</span><span class="token punctuation">,</span><span class="token property">&quot;level&quot;</span><span class="token operator">:</span><span class="token string">&quot;info&quot;</span><span class="token punctuation">,</span><span class="token property">&quot;msg&quot;</span><span class="token operator">:</span>&quot;A group of walrus emerges from the
ocean<span class="token string">&quot;,&quot;</span>size<span class="token string">&quot;:10,&quot;</span>time<span class="token string">&quot;:&quot;</span><span class="token number">2014</span><span class="token number">-03</span><span class="token number">-10</span> <span class="token number">19</span><span class="token operator">:</span><span class="token number">57</span><span class="token operator">:</span><span class="token number">38.562264131</span> <span class="token number">-0400</span> EDT&quot;<span class="token punctuation">}</span>

<span class="token punctuation">{</span><span class="token property">&quot;level&quot;</span><span class="token operator">:</span><span class="token string">&quot;warning&quot;</span><span class="token punctuation">,</span><span class="token property">&quot;msg&quot;</span><span class="token operator">:</span><span class="token string">&quot;The group&#39;s number increased tremendously!&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;number&quot;</span><span class="token operator">:</span><span class="token number">122</span><span class="token punctuation">,</span><span class="token property">&quot;omg&quot;</span><span class="token operator">:</span><span class="token boolean">true</span><span class="token punctuation">,</span><span class="token property">&quot;time&quot;</span><span class="token operator">:</span><span class="token string">&quot;2014-03-10 19:57:38.562471297 -0400 EDT&quot;</span><span class="token punctuation">}</span>

<span class="token punctuation">{</span><span class="token property">&quot;animal&quot;</span><span class="token operator">:</span><span class="token string">&quot;walrus&quot;</span><span class="token punctuation">,</span><span class="token property">&quot;level&quot;</span><span class="token operator">:</span><span class="token string">&quot;info&quot;</span><span class="token punctuation">,</span><span class="token property">&quot;msg&quot;</span><span class="token operator">:</span><span class="token string">&quot;A giant walrus appears!&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;size&quot;</span><span class="token operator">:</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token property">&quot;time&quot;</span><span class="token operator">:</span><span class="token string">&quot;2014-03-10 19:57:38.562500591 -0400 EDT&quot;</span><span class="token punctuation">}</span>

<span class="token punctuation">{</span><span class="token property">&quot;animal&quot;</span><span class="token operator">:</span><span class="token string">&quot;walrus&quot;</span><span class="token punctuation">,</span><span class="token property">&quot;level&quot;</span><span class="token operator">:</span><span class="token string">&quot;info&quot;</span><span class="token punctuation">,</span><span class="token property">&quot;msg&quot;</span><span class="token operator">:</span><span class="token string">&quot;Tremendously sized cow enters the ocean.&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;size&quot;</span><span class="token operator">:</span><span class="token number">9</span><span class="token punctuation">,</span><span class="token property">&quot;time&quot;</span><span class="token operator">:</span><span class="token string">&quot;2014-03-10 19:57:38.562527896 -0400 EDT&quot;</span><span class="token punctuation">}</span>

<span class="token punctuation">{</span><span class="token property">&quot;level&quot;</span><span class="token operator">:</span><span class="token string">&quot;fatal&quot;</span><span class="token punctuation">,</span><span class="token property">&quot;msg&quot;</span><span class="token operator">:</span><span class="token string">&quot;The ice breaks!&quot;</span><span class="token punctuation">,</span><span class="token property">&quot;number&quot;</span><span class="token operator">:</span><span class="token number">100</span><span class="token punctuation">,</span><span class="token property">&quot;omg&quot;</span><span class="token operator">:</span><span class="token boolean">true</span><span class="token punctuation">,</span>
<span class="token property">&quot;time&quot;</span><span class="token operator">:</span><span class="token string">&quot;2014-03-10 19:57:38.562543128 -0400 EDT&quot;</span><span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>使用默认的 <code>log.SetFormatter(&amp;log.TextFormatter{})</code> 当没有附加 TTY 时, 输出与 <a href="http://godoc.org/github.com/kr/logfmt" target="_blank" rel="noopener noreferrer">logfmt</a> 格式兼容:</p><div class="language-text ext-text line-numbers-mode"><pre class="language-text"><code>time=&quot;2015-03-26T01:27:38-04:00&quot; level=debug msg=&quot;Started observing beach&quot; animal=walrus number=8
time=&quot;2015-03-26T01:27:38-04:00&quot; level=info msg=&quot;A group of walrus emerges from the ocean&quot; animal=walrus size=10
time=&quot;2015-03-26T01:27:38-04:00&quot; level=warning msg=&quot;The group&#39;s number increased tremendously!&quot; number=122 omg=true
time=&quot;2015-03-26T01:27:38-04:00&quot; level=debug msg=&quot;Temperature changes&quot; temperature=-4
time=&quot;2015-03-26T01:27:38-04:00&quot; level=panic msg=&quot;It&#39;s over 9000!&quot; animal=orca size=9009
time=&quot;2015-03-26T01:27:38-04:00&quot; level=fatal msg=&quot;The ice breaks!&quot; err=&amp;{0x2082280c0 map[animal:orca size:9009] 2015-03-26 01:27:38.441574009 -0400 EDT panic It&#39;s over 9000!} number=100 omg=true
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>要确保即使在一个 TTY 中，也使用这种 Formatter ，设置 Formatter 如下:</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code>	log<span class="token punctuation">.</span><span class="token function">SetFormatter</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>log<span class="token punctuation">.</span>TextFormatter<span class="token punctuation">{</span>
		DisableColors<span class="token punctuation">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
		FullTimestamp<span class="token punctuation">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
	<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="logging-method-name" tabindex="-1"><a class="header-anchor" href="#logging-method-name" aria-hidden="true">#</a> Logging Method Name</h4><p>如果希望添加一个 <code>method</code> 字段<strong>记录被调用时的方法</strong>，通过以下方式：</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code>log<span class="token punctuation">.</span><span class="token function">SetReportCaller</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><p>这将添加 <code>method</code> 字段，如下所示:</p><div class="language-json ext-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span><span class="token property">&quot;animal&quot;</span><span class="token operator">:</span><span class="token string">&quot;penguin&quot;</span><span class="token punctuation">,</span><span class="token property">&quot;level&quot;</span><span class="token operator">:</span><span class="token string">&quot;fatal&quot;</span><span class="token punctuation">,</span><span class="token property">&quot;method&quot;</span><span class="token operator">:</span><span class="token string">&quot;github.com/sirupsen/arcticcreatures.migrate&quot;</span><span class="token punctuation">,</span><span class="token property">&quot;msg&quot;</span><span class="token operator">:</span><span class="token string">&quot;a penguin swims by&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;time&quot;</span><span class="token operator">:</span><span class="token string">&quot;2014-03-10 19:57:38.562543129 -0400 EDT&quot;</span><span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-text ext-text line-numbers-mode"><pre class="language-text"><code>time=&quot;2015-03-26T01:27:38-04:00&quot; level=fatal method=github.com/sirupsen/arcticcreatures.migrate msg=&quot;a penguin swims by&quot; animal=penguin
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><p>请注意，这会增加开销，成本取决于 Go 的版本，在最近的 1.6 和 1.7 版本的测试中，成本在 20% 和 40% 之间。你可以在你的环境中使用 benchmarks 校验:</p><div class="language-text ext-text line-numbers-mode"><pre class="language-text"><code>go test -bench=.*CallerTracing
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><h4 id="case-sensitivity" tabindex="-1"><a class="header-anchor" href="#case-sensitivity" aria-hidden="true">#</a> Case-sensitivity</h4><p>组织的名称已更改为小写 —— 并且不会再更改回来。如果由于大小写敏感而导致导入冲突，请使用小写导入：<code>github.com/sirupsen/logrus</code>。</p><h4 id="example" tabindex="-1"><a class="header-anchor" href="#example" aria-hidden="true">#</a> Example</h4><p>使用 Logrus 最简单的方法是简单的包级导出 log：</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
  log <span class="token string">&quot;github.com/sirupsen/logrus&quot;</span>
<span class="token punctuation">)</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  log<span class="token punctuation">.</span><span class="token function">WithFields</span><span class="token punctuation">(</span>log<span class="token punctuation">.</span>Fields<span class="token punctuation">{</span>
    <span class="token string">&quot;animal&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;walrus&quot;</span><span class="token punctuation">,</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Info</span><span class="token punctuation">(</span><span class="token string">&quot;A walrus appears&quot;</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>注意，它与 stdlib log API 完全兼容，所以你可以用 <code>log github.com/sirupsen/logrus</code> 替换所有的 <code>log</code> 导入。 你可以定制你想要的一切：</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
  <span class="token string">&quot;os&quot;</span>
  log <span class="token string">&quot;github.com/sirupsen/logrus&quot;</span>
<span class="token punctuation">)</span>

<span class="token keyword">func</span> <span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">// 使用 json 格式替代 ASCII.</span>
  log<span class="token punctuation">.</span><span class="token function">SetFormatter</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>log<span class="token punctuation">.</span>JSONFormatter<span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>

  <span class="token comment">// 输出到 stdout 而不是默认的 stderr</span>
  <span class="token comment">// 可以使任意的 io.Writer, 参考下面的 文件 示例</span>
  log<span class="token punctuation">.</span><span class="token function">SetOutput</span><span class="token punctuation">(</span>os<span class="token punctuation">.</span>Stdout<span class="token punctuation">)</span>

  <span class="token comment">// 仅记录 warning 及以上级别的日志</span>
  log<span class="token punctuation">.</span><span class="token function">SetLevel</span><span class="token punctuation">(</span>log<span class="token punctuation">.</span>WarnLevel<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  log<span class="token punctuation">.</span><span class="token function">WithFields</span><span class="token punctuation">(</span>log<span class="token punctuation">.</span>Fields<span class="token punctuation">{</span>
    <span class="token string">&quot;animal&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;walrus&quot;</span><span class="token punctuation">,</span>
    <span class="token string">&quot;size&quot;</span><span class="token punctuation">:</span>   <span class="token number">10</span><span class="token punctuation">,</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Info</span><span class="token punctuation">(</span><span class="token string">&quot;A group of walrus emerges from the ocean&quot;</span><span class="token punctuation">)</span>

  log<span class="token punctuation">.</span><span class="token function">WithFields</span><span class="token punctuation">(</span>log<span class="token punctuation">.</span>Fields<span class="token punctuation">{</span>
    <span class="token string">&quot;omg&quot;</span><span class="token punctuation">:</span>    <span class="token boolean">true</span><span class="token punctuation">,</span>
    <span class="token string">&quot;number&quot;</span><span class="token punctuation">:</span> <span class="token number">122</span><span class="token punctuation">,</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Warn</span><span class="token punctuation">(</span><span class="token string">&quot;The group&#39;s number increased tremendously!&quot;</span><span class="token punctuation">)</span>

  log<span class="token punctuation">.</span><span class="token function">WithFields</span><span class="token punctuation">(</span>log<span class="token punctuation">.</span>Fields<span class="token punctuation">{</span>
    <span class="token string">&quot;omg&quot;</span><span class="token punctuation">:</span>    <span class="token boolean">true</span><span class="token punctuation">,</span>
    <span class="token string">&quot;number&quot;</span><span class="token punctuation">:</span> <span class="token number">100</span><span class="token punctuation">,</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Fatal</span><span class="token punctuation">(</span><span class="token string">&quot;The ice breaks!&quot;</span><span class="token punctuation">)</span>

  <span class="token comment">// A common pattern is to re-use fields between logging statements by re-using</span>
  <span class="token comment">// the logrus.Entry returned from WithFields()</span>
  contextLogger <span class="token operator">:=</span> log<span class="token punctuation">.</span><span class="token function">WithFields</span><span class="token punctuation">(</span>log<span class="token punctuation">.</span>Fields<span class="token punctuation">{</span>
    <span class="token string">&quot;common&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;this is a common field&quot;</span><span class="token punctuation">,</span>
    <span class="token string">&quot;other&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;I also should be logged always&quot;</span><span class="token punctuation">,</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span>

  contextLogger<span class="token punctuation">.</span><span class="token function">Info</span><span class="token punctuation">(</span><span class="token string">&quot;I&#39;ll be logged with common and other field&quot;</span><span class="token punctuation">)</span>
  contextLogger<span class="token punctuation">.</span><span class="token function">Info</span><span class="token punctuation">(</span><span class="token string">&quot;Me too&quot;</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>对于更高级的用法，例如从同一个应用程序记录日志到多个位置，可以创建 <code>logrus</code> 的实例:</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
  <span class="token string">&quot;os&quot;</span>
  <span class="token string">&quot;github.com/sirupsen/logrus&quot;</span>
<span class="token punctuation">)</span>

<span class="token comment">// New() 函数来创建一个 logrus 的实例</span>
<span class="token comment">// 你可以有多个示例</span>
<span class="token keyword">var</span> log <span class="token operator">=</span> logrus<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">// 为当前 logrus 实例设置消息的输出</span>
  log<span class="token punctuation">.</span>Out <span class="token operator">=</span> os<span class="token punctuation">.</span>Stdout

  <span class="token comment">// 可以设置 logrus 实例的输出到任意 io.writer</span>
  <span class="token comment">// file, err := os.OpenFile(&quot;logrus.log&quot;, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)</span>
  <span class="token comment">// if err == nil {</span>
  <span class="token comment">//  log.Out = file</span>
  <span class="token comment">// } else {</span>
  <span class="token comment">//  log.Info(&quot;Failed to log to file, using default stderr&quot;)</span>
  <span class="token comment">// }</span>

  log<span class="token punctuation">.</span><span class="token function">WithFields</span><span class="token punctuation">(</span>logrus<span class="token punctuation">.</span>Fields<span class="token punctuation">{</span>
    <span class="token string">&quot;animal&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;walrus&quot;</span><span class="token punctuation">,</span>
    <span class="token string">&quot;size&quot;</span><span class="token punctuation">:</span>   <span class="token number">10</span><span class="token punctuation">,</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Info</span><span class="token punctuation">(</span><span class="token string">&quot;A group of walrus emerges from the ocean&quot;</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="fields" tabindex="-1"><a class="header-anchor" href="#fields" aria-hidden="true">#</a> Fields</h4><p>Logrus 不推荐使用冗长的消息来记录运行信息。 推荐使用 Fields 来进行精细化的、结构化的信息记录。例如: 替换 <code>log.Fatalf(&quot;Failed to send event %s to topic %s with key %d&quot;)</code>, 应该记录更容易发现的：</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code>log<span class="token punctuation">.</span><span class="token function">WithFields</span><span class="token punctuation">(</span>log<span class="token punctuation">.</span>Fields<span class="token punctuation">{</span>
  <span class="token string">&quot;event&quot;</span><span class="token punctuation">:</span> event<span class="token punctuation">,</span>
  <span class="token string">&quot;topic&quot;</span><span class="token punctuation">:</span> topic<span class="token punctuation">,</span>
  <span class="token string">&quot;key&quot;</span><span class="token punctuation">:</span> key<span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Fatal</span><span class="token punctuation">(</span><span class="token string">&quot;Failed to send event&quot;</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>我们发现，这个 API 迫使你考虑如何产生更有用的日志消息。我们已经经历无数的情况，在日志声明中添加一个字段可以节省很多时间。 <code>WithFields</code> 调用不是必须的。</p><p>一般来说，使用 Logrus 中的任何 <code>printf</code>- family 函数都该被视为应该添加字段的提示，但是，仍然可以使用 Logrus 中的 <code>printf</code> - family 函数。</p><h4 id="default-fields" tabindex="-1"><a class="header-anchor" href="#default-fields" aria-hidden="true">#</a> Default Fields</h4><p>通常，将字段<em>总是</em>附加到应用程序的日志声明中或应用程序的一部分是有帮助的。例如，你可能希望始终在请求上下文中记录 <code>request_id</code> 和 <code>user_ip</code>。 而不是在每行都写 <code>log.WithFields(log.Fields{&quot;request_id&quot;: request_id, &quot;user_ip&quot;: user_ip})</code>， 你可以创建一个 <code>logrus.Entry</code>:</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code>requestLogger <span class="token operator">:=</span> log<span class="token punctuation">.</span><span class="token function">WithFields</span><span class="token punctuation">(</span>log<span class="token punctuation">.</span>Fields<span class="token punctuation">{</span><span class="token string">&quot;request_id&quot;</span><span class="token punctuation">:</span> request_id<span class="token punctuation">,</span> <span class="token string">&quot;user_ip&quot;</span><span class="token punctuation">:</span> user_ip<span class="token punctuation">}</span><span class="token punctuation">)</span>
requestLogger<span class="token punctuation">.</span><span class="token function">Info</span><span class="token punctuation">(</span><span class="token string">&quot;something happened on that request&quot;</span><span class="token punctuation">)</span> # will log request_id and user_ip
requestLogger<span class="token punctuation">.</span><span class="token function">Warn</span><span class="token punctuation">(</span><span class="token string">&quot;something not great happened&quot;</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="hooks" tabindex="-1"><a class="header-anchor" href="#hooks" aria-hidden="true">#</a> Hooks</h4><p>你可以添加日志级别的 hooks。例如，向异常跟踪服务发送 <code>Error</code>，<code>Fatal</code> 和 <code>Panic</code>、<code>info</code> 到 StatsD 或同时将日志发送到多个位置，例如 syslog。</p><p>Logrus <a href="https://github.com/sirupsen/logrus/tree/master/hooks" target="_blank" rel="noopener noreferrer">内建的 hooks</a>。在 <code>init</code> 中添加这些或自定义 hook：</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">import</span> <span class="token punctuation">(</span>
  log <span class="token string">&quot;github.com/sirupsen/logrus&quot;</span>
  <span class="token string">&quot;gopkg.in/gemnasium/logrus-airbrake-hook.v2&quot;</span> <span class="token comment">// the package is named &quot;airbrake&quot;</span>
  logrus_syslog <span class="token string">&quot;github.com/sirupsen/logrus/hooks/syslog&quot;</span>
  <span class="token string">&quot;log/syslog&quot;</span>
<span class="token punctuation">)</span>

<span class="token keyword">func</span> <span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

  <span class="token comment">// Use the Airbrake hook to report errors that have Error severity or above to</span>
  <span class="token comment">// an exception tracker. You can create custom hooks, see the Hooks section.</span>
  log<span class="token punctuation">.</span><span class="token function">AddHook</span><span class="token punctuation">(</span>airbrake<span class="token punctuation">.</span><span class="token function">NewHook</span><span class="token punctuation">(</span><span class="token number">123</span><span class="token punctuation">,</span> <span class="token string">&quot;xyz&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;production&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

  hook<span class="token punctuation">,</span> err <span class="token operator">:=</span> logrus_syslog<span class="token punctuation">.</span><span class="token function">NewSyslogHook</span><span class="token punctuation">(</span><span class="token string">&quot;udp&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;localhost:514&quot;</span><span class="token punctuation">,</span> syslog<span class="token punctuation">.</span>LOG_INFO<span class="token punctuation">,</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">)</span>
  <span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
    log<span class="token punctuation">.</span><span class="token function">Error</span><span class="token punctuation">(</span><span class="token string">&quot;Unable to connect to local syslog daemon&quot;</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
    log<span class="token punctuation">.</span><span class="token function">AddHook</span><span class="token punctuation">(</span>hook<span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>注意: Syslog 钩子还支持连接到本地 Syslog(例如。<code>/dev/log</code> 或 <code>/var/run/syslog</code> 或 <code>/var/run/log</code>)。 更多详情查看 <a href="/posts/2023/4/docs/logrus/docs/syslog/" class="">syslog hook README</a>.</p><p>目前已知的服务 hooks 列表可以在这个 <a href="https://github.com/sirupsen/logrus/wiki/Hooks" target="_blank" rel="noopener noreferrer">wiki page</a> 中找到。</p><h4 id="level-logging" tabindex="-1"><a class="header-anchor" href="#level-logging" aria-hidden="true">#</a> Level logging</h4><p>Logrus 有七个日志等级: Trace, Debug, Info, Warning, Error, Fatal 和 Panic。</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code>log<span class="token punctuation">.</span><span class="token function">Trace</span><span class="token punctuation">(</span><span class="token string">&quot;Something very low level.&quot;</span><span class="token punctuation">)</span>
log<span class="token punctuation">.</span><span class="token function">Debug</span><span class="token punctuation">(</span><span class="token string">&quot;Useful debugging information.&quot;</span><span class="token punctuation">)</span>
log<span class="token punctuation">.</span><span class="token function">Info</span><span class="token punctuation">(</span><span class="token string">&quot;Something noteworthy happened!&quot;</span><span class="token punctuation">)</span>
log<span class="token punctuation">.</span><span class="token function">Warn</span><span class="token punctuation">(</span><span class="token string">&quot;You should probably take a look at this.&quot;</span><span class="token punctuation">)</span>
log<span class="token punctuation">.</span><span class="token function">Error</span><span class="token punctuation">(</span><span class="token string">&quot;Something failed but I&#39;m not quitting.&quot;</span><span class="token punctuation">)</span>
<span class="token comment">// 输出日志之后，调用 os.Exit(1)</span>
log<span class="token punctuation">.</span><span class="token function">Fatal</span><span class="token punctuation">(</span><span class="token string">&quot;Bye.&quot;</span><span class="token punctuation">)</span>
<span class="token comment">// 输出日志之后，调用 panic()</span>
log<span class="token punctuation">.</span><span class="token function">Panic</span><span class="token punctuation">(</span><span class="token string">&quot;I&#39;m bailing.&quot;</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以为一个 <code>Logger</code> 设置一个日志等级，然后它只会记录对应等级或以上的条目：</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// 默认情况下会记录 info 及以上等级 (warn, error, fatal, panic) 的日志</span>
log<span class="token punctuation">.</span><span class="token function">SetLevel</span><span class="token punctuation">(</span>log<span class="token punctuation">.</span>InfoLevel<span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div></div></div><p>设置 <code>log.Level = logrus.DebugLevel</code> 在 debug 或 verbose 下是非常有用的。</p><h4 id="entries" tabindex="-1"><a class="header-anchor" href="#entries" aria-hidden="true">#</a> Entries</h4><p>除了使用 <code>WithField</code> 或 <code>WithFields</code> 添加字段外，有些字段还会自动添加到所有日志事件中：</p><ol><li><code>time</code>。创建条目时的时间戳。</li><li><code>msg</code>。在 <code>AddFields</code> 调用之后，日志消息传递给 <code>Info,Warn,Error,Fatal,Panic}</code>。 例如 <code>Failed to send event.</code></li><li><code>level</code>。日志等级。例如 <code>info</code>。</li></ol><h4 id="environments" tabindex="-1"><a class="header-anchor" href="#environments" aria-hidden="true">#</a> Environments</h4><p>Logrus 没有环境的概念。</p><p>如果你希望 hooks 和 formatters 只在特定的环境中使用，你需要自己处理它。 例如，如果你的应有有一个全局变量 <code>Environment</code>，这是一个表示环境的字符串，你可以:</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">import</span> <span class="token punctuation">(</span>
  log <span class="token string">&quot;github.com/sirupsen/logrus&quot;</span>
<span class="token punctuation">)</span>

<span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">// do something here to set environment depending on an environment variable</span>
  <span class="token comment">// or command-line flag</span>
  <span class="token keyword">if</span> Environment <span class="token operator">==</span> <span class="token string">&quot;production&quot;</span> <span class="token punctuation">{</span>
    log<span class="token punctuation">.</span><span class="token function">SetFormatter</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>log<span class="token punctuation">.</span>JSONFormatter<span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
    <span class="token comment">// The TextFormatter is default, you don&#39;t actually have to do this.</span>
    log<span class="token punctuation">.</span><span class="token function">SetFormatter</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>log<span class="token punctuation">.</span>TextFormatter<span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>这个配置就是如何使用 <code>logrus</code>，但是，JSON 在生产中通常只在使用诸如 Splunk或Logstash 此类的工具进行日 志聚合时才有用。</p><h4 id="formatters" tabindex="-1"><a class="header-anchor" href="#formatters" aria-hidden="true">#</a> Formatters</h4><p>内建的 formatters:</p><ul><li><code>logrus.TextFormatter</code>。如果 stdout 是一个 TTY，则使用颜色记录事件，否则没有颜色。 <ul><li><em>Note:</em> 在没有 TTY 是强制使用带颜色的输出，设置 <code>ForceColors</code> 字段为 <code>true</code>。 在 TTY 中强制不使用带颜色的输出，设置 <code>DisableColors</code> 字段为 <code>true</code>。 对于 windows，查看 <a href="https://github.com/mattn/go-colorable" target="_blank" rel="noopener noreferrer">github.com/mattn/go-colorable</a>。</li><li>启用 colors 后，默认情况下 levels 被截断为 4 个字符。 禁用截断设置 <code>DisableLevelTruncation</code> 字段为 <code>true</code>。在所有层的宽度都相同的情况下，直观地扫描列通常是有帮助的。</li><li>当输出到 TTY 时, 所有 levels 的宽度都相同对于直观地扫描列通常是有帮助的。设置 <code>PadLevelText</code> 字段 为 <code>true</code>, 通过向水平文本添加填充，来启用该行为。</li><li><a href="https://godoc.org/github.com/sirupsen/logrus#TextFormatter" target="_blank" rel="noopener noreferrer">generated docs</a> 列出了所有选项。</li></ul></li><li><code>logrus.JSONFormatter</code>. 以 JSON 格式记录。 <ul><li><a href="https://godoc.org/github.com/sirupsen/logrus#JSONFormatter" target="_blank" rel="noopener noreferrer">generated docs</a> 列出了所有选项。</li></ul></li></ul><p>第三方的 formatters:</p><ul><li><a href="https://github.com/joonix/log" target="_blank" rel="noopener noreferrer"><code>FluentdFormatter</code></a>. 格式化条目以便 Kubernetes 和 Google Container 引擎解析。</li><li><a href="https://github.com/fabienm/go-logrus-formatters" target="_blank" rel="noopener noreferrer"><code>GELF</code></a>. 格式化条目，使它们符合 Graylog 的格式 <a href="http://docs.graylog.org/en/2.4/pages/gelf.html" target="_blank" rel="noopener noreferrer">GELF 1.1 specification</a>。</li><li><a href="https://github.com/bshuster-repo/logrus-logstash-hook" target="_blank" rel="noopener noreferrer"><code>logstash</code></a>. 将字段记录为 <a href="http://logstash.net" target="_blank" rel="noopener noreferrer">Logstash</a> Envents.</li><li><a href="https://github.com/x-cray/logrus-prefixed-formatter" target="_blank" rel="noopener noreferrer"><code>prefixed</code></a>. 显示日志条目源。</li><li><a href="https://github.com/aybabtme/logzalgo" target="_blank" rel="noopener noreferrer"><code>zalgo</code></a></li><li><a href="https://github.com/antonfisher/nested-logrus-formatter" target="_blank" rel="noopener noreferrer"><code>nested-logrus-formatter</code></a>. 将 logrus 字段转换为嵌套结构。</li></ul><p>你可以通过实现 <code>Formatter</code> interface 来定义自己的 Formatter，需要提供 <code>Format</code> 方法。 <code>Format</code>接受一个 <code>*Entry</code>。<code>entry.Data</code> 是一个 <code>Fields</code> 类型 (<code>map[string]interface{}</code>) 包含所有字段和默认字段(参见上面的 Entries 部分):</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">type</span> MyJSONFormatter <span class="token keyword">struct</span> <span class="token punctuation">{</span>
<span class="token punctuation">}</span>

log<span class="token punctuation">.</span><span class="token function">SetFormatter</span><span class="token punctuation">(</span><span class="token function">new</span><span class="token punctuation">(</span>MyJSONFormatter<span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token keyword">func</span> <span class="token punctuation">(</span>f <span class="token operator">*</span>MyJSONFormatter<span class="token punctuation">)</span> <span class="token function">Format</span><span class="token punctuation">(</span>entry <span class="token operator">*</span>Entry<span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">// Note this doesn&#39;t include Time, Level and Message which are available on</span>
  <span class="token comment">// the Entry. Consult `godoc` on information about those fields or read the</span>
  <span class="token comment">// source of the official loggers.</span>
  serialized<span class="token punctuation">,</span> err <span class="token operator">:=</span> json<span class="token punctuation">.</span><span class="token function">Marshal</span><span class="token punctuation">(</span>entry<span class="token punctuation">.</span>Data<span class="token punctuation">)</span>
    <span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
      <span class="token keyword">return</span> <span class="token boolean">nil</span><span class="token punctuation">,</span> fmt<span class="token punctuation">.</span><span class="token function">Errorf</span><span class="token punctuation">(</span><span class="token string">&quot;Failed to marshal fields to JSON, %v&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
  <span class="token keyword">return</span> <span class="token function">append</span><span class="token punctuation">(</span>serialized<span class="token punctuation">,</span> <span class="token char">&#39;\n&#39;</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token boolean">nil</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="logger-as-an-io-writer" tabindex="-1"><a class="header-anchor" href="#logger-as-an-io-writer" aria-hidden="true">#</a> Logger as an <code>io.Writer</code></h4><p>Logrus 可以转换成一个 <code>io.Writer</code>。 writer 是一个 <code>io.Pipe</code> 的结束，并且你需要关闭它。</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code>w <span class="token operator">:=</span> logger<span class="token punctuation">.</span><span class="token function">Writer</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">defer</span> w<span class="token punctuation">.</span><span class="token function">Close</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

srv <span class="token operator">:=</span> http<span class="token punctuation">.</span>Server<span class="token punctuation">{</span>
    <span class="token comment">// create a stdlib log.Logger that writes to</span>
    <span class="token comment">// logrus.Logger.</span>
    ErrorLog<span class="token punctuation">:</span> log<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span>w<span class="token punctuation">,</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>被写入 writer 的每一行会以一般的方式打印出来，使用 formatters 和 hooks。这些条目的级别是 <code>info</code>。</p><p>这意味着我们可以轻易的覆盖标准库的 log:</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code>logger <span class="token operator">:=</span> logrus<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
logger<span class="token punctuation">.</span>Formatter <span class="token operator">=</span> <span class="token operator">&amp;</span>logrus<span class="token punctuation">.</span>JSONFormatter<span class="token punctuation">{</span><span class="token punctuation">}</span>

<span class="token comment">// Use logrus for standard log output</span>
<span class="token comment">// Note that `log` here references stdlib&#39;s log</span>
<span class="token comment">// Not logrus imported under the name `log`.</span>
log<span class="token punctuation">.</span><span class="token function">SetOutput</span><span class="token punctuation">(</span>logger<span class="token punctuation">.</span><span class="token function">Writer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="rotation" tabindex="-1"><a class="header-anchor" href="#rotation" aria-hidden="true">#</a> Rotation</h4><p>Logrus 并没有提供 Log rotation。Log rotation 需要一个外部的程序完成 (比如 <code>logrotate(8)</code>) 可以压缩和删除旧日志条目。 它不应该是应用程序级 logger 的特性。</p><h4 id="tools" tabindex="-1"><a class="header-anchor" href="#tools" aria-hidden="true">#</a> Tools</h4><table><thead><tr><th>Tool</th><th>Description</th></tr></thead><tbody><tr><td><a href="https://github.com/gogap/logrus_mate" target="_blank" rel="noopener noreferrer">Logrus Mate</a></td><td>Logrus mate is a tool for Logrus to manage loggers, you can initial logger&#39;s level, hook and formatter by config file, the logger will generated with different config at different environment.</td></tr><tr><td><a href="https://github.com/heirko/go-contrib/tree/master/logrusHelper" target="_blank" rel="noopener noreferrer">Logrus Viper Helper</a></td><td>An Helper around Logrus to wrap with spf13/Viper to load configuration with fangs! And to simplify Logrus configuration use some behavior of <a href="https://github.com/gogap/logrus_mate" target="_blank" rel="noopener noreferrer">Logrus Mate</a>. <a href="https://github.com/heirko/iris-contrib/blob/master/middleware/logrus-logger/example" target="_blank" rel="noopener noreferrer">sample</a></td></tr></tbody></table><h4 id="testing" tabindex="-1"><a class="header-anchor" href="#testing" aria-hidden="true">#</a> Testing</h4><p>Logrus 有一个内置的工具，用于断言日志消息的存在。这是通过 <code>test</code> hook 实现的，并提供:</p><ul><li>现有的 logger 的装饰器 (<code>test.NewLocal</code> 和 <code>test.NewGlobal</code>) 只是添加 <code>test</code> hook</li><li>一个测试的 logger (<code>test.NewNullLogger</code>) 只记录日志消息 (并且不做任何输出):</li></ul><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">import</span><span class="token punctuation">(</span>
  <span class="token string">&quot;github.com/sirupsen/logrus&quot;</span>
  <span class="token string">&quot;github.com/sirupsen/logrus/hooks/test&quot;</span>
  <span class="token string">&quot;github.com/stretchr/testify/assert&quot;</span>
  <span class="token string">&quot;testing&quot;</span>
<span class="token punctuation">)</span>

<span class="token keyword">func</span> <span class="token function">TestSomething</span><span class="token punctuation">(</span>t<span class="token operator">*</span>testing<span class="token punctuation">.</span>T<span class="token punctuation">)</span><span class="token punctuation">{</span>
  logger<span class="token punctuation">,</span> hook <span class="token operator">:=</span> test<span class="token punctuation">.</span><span class="token function">NewNullLogger</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  logger<span class="token punctuation">.</span><span class="token function">Error</span><span class="token punctuation">(</span><span class="token string">&quot;Helloerror&quot;</span><span class="token punctuation">)</span>

  assert<span class="token punctuation">.</span><span class="token function">Equal</span><span class="token punctuation">(</span>t<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>hook<span class="token punctuation">.</span>Entries<span class="token punctuation">)</span><span class="token punctuation">)</span>
  assert<span class="token punctuation">.</span><span class="token function">Equal</span><span class="token punctuation">(</span>t<span class="token punctuation">,</span> logrus<span class="token punctuation">.</span>ErrorLevel<span class="token punctuation">,</span> hook<span class="token punctuation">.</span><span class="token function">LastEntry</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>Level<span class="token punctuation">)</span>
  assert<span class="token punctuation">.</span><span class="token function">Equal</span><span class="token punctuation">(</span>t<span class="token punctuation">,</span> <span class="token string">&quot;Helloerror&quot;</span><span class="token punctuation">,</span> hook<span class="token punctuation">.</span><span class="token function">LastEntry</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>Message<span class="token punctuation">)</span>

  hook<span class="token punctuation">.</span><span class="token function">Reset</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  assert<span class="token punctuation">.</span><span class="token function">Nil</span><span class="token punctuation">(</span>t<span class="token punctuation">,</span> hook<span class="token punctuation">.</span><span class="token function">LastEntry</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="fatal-handlers" tabindex="-1"><a class="header-anchor" href="#fatal-handlers" aria-hidden="true">#</a> Fatal handlers</h4><p>Logrus 可以注册一个或多个函数，这些函数将在 <code>fatal</code> 级别的日志记录时时被调用。 注册的函数会在 logrus 执行 <code>os.Exit(1)</code> 之前调用。调用者需要优雅的 shutdown 时很有用。 与调用一个 <code>panic(&quot;Something went wrong...&quot;)</code> 不同， <code>panic(&quot;Something went wrong...&quot;)</code> 可以通过延迟的 <code>recover</code> 拦截，对 <code>os.Exit(1)</code> 的调用，不能拦截。</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token operator">...</span>
handler <span class="token operator">:=</span> <span class="token keyword">func</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">// gracefully shutdown something...</span>
<span class="token punctuation">}</span>
logrus<span class="token punctuation">.</span><span class="token function">RegisterExitHandler</span><span class="token punctuation">(</span>handler<span class="token punctuation">)</span>
<span class="token operator">...</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="thread-safety" tabindex="-1"><a class="header-anchor" href="#thread-safety" aria-hidden="true">#</a> Thread safety</h4><p>默认情况下，Logger 受以个 mutex 的保护，用于并发写操作。这个 mutex 在调用 hook 和写入日志时被持有。 如果确定不需要这样的锁，可以调用 <code>log .SetNoLock()</code> 来禁用锁。</p><p>不需要锁的情况:</p><ul><li><p>你没有注册 hooks，或者调用 hook 已经是线程安全的。</p></li><li><p>写入 <code>logger.Out</code> 已经是线程安全的, 例如:</p><ol><li><p><code>logger.Out</code> 已经别锁保护。</p></li><li><p><code>logger.Out</code> 是一个通过标志 <code>O_APPEND</code> 打开的 <code>os.File</code> handler, 并且 每次写入都小于 4k. (这允许 多线程/多进程 写)</p><p>(参考 http://www.notthewizard.com/2014/06/17/are-files-appends-really-atomic/)</p></li></ol></li></ul><!--]--><!--[--><!--]--></div><footer class="page-meta"><!----><div class="meta-item last-updated"><span class="meta-item-label">Last Updated: </span><!----></div><!----></footer><!----><!--[--><!--]--><!----></main><!--]--></div><div class="search-page" role="search"><span class="search-close"><svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 448 512" width="28" height="28" fill="currentColor"><path d="M224 416c-8.188 0-16.38-3.125-22.62-9.375l-192-192c-12.5-12.5-12.5-32.75 0-45.25s32.75-12.5 45.25 0L224 338.8l169.4-169.4c12.5-12.5 32.75-12.5 45.25 0s12.5 32.75 0 45.25l-192 192C240.4 412.9 232.2 416 224 416z"></path></svg></span><div class="gungnir-search-box"><input placeholder="$ grep ..." autocomplete="off" spellcheck="false" value><!----></div></div><div class="menu-btn-container"><div class="menu-btn-wrapper"><div class="menu-btn"><div style="" class="menu-btn-icon"><span></span><span></span><span></span></div><div style="display:none;" class="menu-text">0</div><svg class="menu-progress"><circle class="menu-border" cx="50%" cy="50%" r="48%" style="stroke-dasharray:0% 314.15926%;"></circle></svg></div><div class="menu-btn-child-wrapper"><div class="toggle-dark-button menu-btn-child" title="toggle dark mode"><svg class="ov-icon" style="font-size:1.2em;display:none;" aria-hidden="true" width="19.2" height="19.2" viewbox="-43.52 -43.52 599.04 599.04" fill="currentColor"><path d="M256 160c-52.9 0-96 43.1-96 96s43.1 96 96 96 96-43.1 96-96-43.1-96-96-96zm246.4 80.5l-94.7-47.3 33.5-100.4c4.5-13.6-8.4-26.5-21.9-21.9l-100.4 33.5-47.4-94.8c-6.4-12.8-24.6-12.8-31 0l-47.3 94.7L92.7 70.8c-13.6-4.5-26.5 8.4-21.9 21.9l33.5 100.4-94.7 47.4c-12.8 6.4-12.8 24.6 0 31l94.7 47.3-33.5 100.5c-4.5 13.6 8.4 26.5 21.9 21.9l100.4-33.5 47.3 94.7c6.4 12.8 24.6 12.8 31 0l47.3-94.7 100.4 33.5c13.6 4.5 26.5-8.4 21.9-21.9l-33.5-100.4 94.7-47.3c13-6.5 13-24.7.2-31.1zm-155.9 106c-49.9 49.9-131.1 49.9-181 0-49.9-49.9-49.9-131.1 0-181 49.9-49.9 131.1-49.9 181 0 49.9 49.9 49.9 131.1 0 181z"/></svg><svg class="ov-icon" style="font-size:1.2em;display:none;" aria-hidden="true" width="19.2" height="19.2" viewbox="-43.52 -43.52 599.04 599.04" fill="currentColor"><path d="M283.211 512c78.962 0 151.079-35.925 198.857-94.792 7.068-8.708-.639-21.43-11.562-19.35-124.203 23.654-238.262-71.576-238.262-196.954 0-72.222 38.662-138.635 101.498-174.394 9.686-5.512 7.25-20.197-3.756-22.23A258.156 258.156 0 00283.211 0c-141.309 0-256 114.511-256 256 0 141.309 114.511 256 256 256z"/></svg><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="-43.52 -43.52 599.04 599.04" fill="currentColor"><path d="M224 96l16-32 32-16-32-16-16-32-16 32-32 16 32 16 16 32zM80 160l26.66-53.33L160 80l-53.34-26.67L80 0 53.34 53.33 0 80l53.34 26.67L80 160zm352 128l-26.66 53.33L352 368l53.34 26.67L432 448l26.66-53.33L512 368l-53.34-26.67L432 288zm70.62-193.77L417.77 9.38C411.53 3.12 403.34 0 395.15 0c-8.19 0-16.38 3.12-22.63 9.38L9.38 372.52c-12.5 12.5-12.5 32.76 0 45.25l84.85 84.85c6.25 6.25 14.44 9.37 22.62 9.37 8.19 0 16.38-3.12 22.63-9.37l363.14-363.15c12.5-12.48 12.5-32.75 0-45.24zM359.45 203.46l-50.91-50.91 86.6-86.6 50.91 50.91-86.6 86.6z"/></svg></div><div class="menu-btn-child"><svg class="ov-icon" style="font-size:1.7999999999999998em;" aria-hidden="true" width="28.799999999999997" height="28.799999999999997" viewbox="-75.52 -43.52 599.04 599.04" fill="currentColor"><path d="M207.029 381.476L12.686 187.132c-9.373-9.373-9.373-24.569 0-33.941l22.667-22.667c9.357-9.357 24.522-9.375 33.901-.04L224 284.505l154.745-154.021c9.379-9.335 24.544-9.317 33.901.04l22.667 22.667c9.373 9.373 9.373 24.569 0 33.941L240.971 381.476c-9.373 9.372-24.569 9.372-33.942 0z"/></svg></div><div class="menu-btn-child"><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="-75.52 -43.52 599.04 599.04" fill="currentColor"><path d="M240.971 130.524l194.343 194.343c9.373 9.373 9.373 24.569 0 33.941l-22.667 22.667c-9.357 9.357-24.522 9.375-33.901.04L224 227.495 69.255 381.516c-9.379 9.335-24.544 9.317-33.901-.04l-22.667-22.667c-9.373-9.373-9.373-24.569 0-33.941L207.03 130.525c9.372-9.373 24.568-9.373 33.941-.001z"/></svg></div><!----><div class="toggle-sidebar-button menu-btn-child menu-btn-sidebar" title="toggle sidebar" aria-expanded="false" role="button" tabindex="0"><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="-1.6 -1.6 19.2 19.2" fill="currentColor"><path d="M14 2a1 1 0 011 1v10a1 1 0 01-1 1H2a1 1 0 01-1-1V3a1 1 0 011-1h12zM2 1a2 2 0 00-2 2v10a2 2 0 002 2h12a2 2 0 002-2V3a2 2 0 00-2-2H2z"/><path d="M3 4a1 1 0 011-1h2a1 1 0 011 1v8a1 1 0 01-1 1H4a1 1 0 01-1-1V4z"/></svg></div></div></div></div><!----></div><!--]--></div>
    <script type="module" src="/assets/app.80e5dc69.js" defer></script>
  </body>
</html>
